#ifndef STRATEGY_USER_2_H_
#define STRATEGY_USER_2_H_
#pragma once
#include "strategy_sys.h"

typedef struct userdata_stra2
{
	userdata_stra2()
	{
		vol = 0;
		stopcnt = 0;
	}
	unsigned int vol;
	unsigned int stopcnt;

}userdata_stra2;


class CInstMap;


class CStrategyUser2: public CStrategySys
{
public:

	CStrategyUser2(string threadid);


	virtual ~CStrategyUser2(){};

private:

	virtual bool Init(BarInfo* pbar, map<string, vector<BarSeries*>>& mapuserbar, char* puserdata = NULL);

	virtual bool Destroy(BarInfo* pbar, map<string, vector<BarSeries*>>& mapuserbar);

	virtual bool Edit(BarInfo* pbar, char* puserdata);

	virtual void Run(BarData* ptickdata, char* inst);

	virtual void OnOrder(CThostFtdcOrderField *pOrder);

	virtual void OnTrade(CThostFtdcTradeField *pTrade);

	virtual void OnCancelOrder(CThostFtdcOrderField *pOrder);

	virtual void OnCancelOrderFailed(CThostFtdcInputOrderActionField *pCancelOrderFailed);

	virtual void OnStrategyCom(unsigned int size, char* pdata){};

private:

	void NotTimeout(CInstMap* pinstmap);

	void Timingout(CInstMap* pinstmap);

	bool Open(CInstMap* pinstmap);

	typedef enum CloseToday_Type
	{
		notcancel = 1, cancelfailed, cancelsuccess
	}CloseToday_Type;
	bool CloseToday(CInstMap* pinstmap, CloseToday_Type closetype);

	bool Cancel(CInstMap* pinstmap);

	bool CancelAndClosetoday(char* prefix, CInstMap* pinstmap);

	void Print_SyncOrder(CThostFtdcOrderField *order);


private:

	map<string , CInstMap* > inst_map;

	

};


class CInstMap
{
public:

	CInstMap(const char* pinst, int vol, double _pricetick, unsigned int _stopcnt_cl, BarSeries* _pbarser);

	~CInstMap();

	void SetPrice(double _baseprice);

	bool PriceFire(double lastprice, double UpperLimitPrice, double LowerLimitPrice);

	bool TimeFire(double lastprice);

public:

	char inst[10];

	double pricetick;



	double tradeprice;

	unsigned int tradejump;

	int reqvol; 

	int direct;//1 for up, 2 for down

	char offset; //#define THOST_FTDC_OF_Open '0' // #define THOST_FTDC_OF_CloseToday '3'

	TThostFtdcOrderRefType orderref;



	int state;

	unsigned int cnt_cl;

	unsigned int stopcnt_cl;

	bool binitbase;

	bool btimeout;

	double baseprice;
	
	double uplimitprice;

	double downlimitprice;

	const unsigned int basejump;

	const unsigned int openjump;

	const unsigned int closejump;

	const unsigned int opendistance;

	BarSeries* pbarser;


};



#endif